﻿using System;
using System.Windows.Forms;
using HossMvp.Bootstrap;
using HossMvp.Core;

namespace HossMvp.WinForms
{
    public class WinFormsHossMvpBootstrapper : HossMvpBootstrapper
    {
        public void Start<F, P>(string exitCommandName, ModelManager modelManager = null, ViewManager viewManager = null)
            where F : Form
            where P : AbstractPresenter
        {
            var presenter = (P)Activator.CreateInstance(typeof(P), modelManager ?? new ModelManager(), viewManager ?? new ViewManager());
            var mainForm = (F)Activator.CreateInstance(typeof(F), presenter);
            var exit = presenter.ViewManager.GetCommand(exitCommandName);
            exit.Bind(mainForm, WinFormsEventTypeConstants.FormClosing);
            Application.Run(mainForm);
        }

        void mainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            System.Text.StringBuilder messageBoxCS = new System.Text.StringBuilder();
            messageBoxCS.AppendFormat("{0} = {1}", "CloseReason", e.CloseReason);
            messageBoxCS.AppendLine();
            messageBoxCS.AppendFormat("{0} = {1}", "Cancel", e.Cancel);
            messageBoxCS.AppendLine();
            MessageBox.Show(messageBoxCS.ToString(), "FormClosing Event");
        }
    }
}
